﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RectTransformBehavior : MonoBehaviour {
    public RectTransform rectTransform
    {
        get
        {
            if (m_rectTransform == null)
                m_rectTransform = gameObject.GetComponent<RectTransform>();
            return m_rectTransform;
        }
    }
    private RectTransform m_rectTransform;
    public bool SynchronizeWidth;
    public bool SynchronizeHeight;
    public RectTransform WidthSynchronizeGameObject;
    public RectTransform HeightSynchronizeGameObject;
    private void OnRectTransformDimensionsChange()
    {
        OnRTDimensionsChange();
    }
    public void OnRTDimensionsChange()
    {
        if (SynchronizeWidth && WidthSynchronizeGameObject != null)
        {
            WidthSynchronizeGameObject.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, rectTransform.rect.width);
        }
        if (SynchronizeHeight && HeightSynchronizeGameObject != null)
        {
            HeightSynchronizeGameObject.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, rectTransform.rect.height);
        }
    }
}
